﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class ClientsByClientsGroup : ClassManager.SessionManagerPage
    {
        private void GetAllClientsByClientsGroupID(int ClientsGroupID)
        {
            GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.ClientsByClientsGroups> ClientsByClientsGroupsEntites = GPSTrackingDBV2.Data.DataRepository.ClientsByClientsGroupsProvider.GetByClientId(ClientsGroupID);
            GPSTrackingDBV2.Data.DataRepository.ClientsByClientsGroupsProvider.DeepLoad(ClientsByClientsGroupsEntites, true, GPSTrackingDBV2.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingDBV2.Entities.Clients));

           ClientsGroupsGridLinks.DataSource =ClientsByClientsGroupsEntites;
           ClientsGroupsGridLinks.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            uscClientsGroupSearch.OnAcceptEvent += uscClientsGroupSearch_OnAcceptEvent;
            uscClientSearch.OnAddClientsEvent += uscClientSearch_OnAddAdminEvent;
        }

        void uscClientsGroupSearch_OnAcceptEvent(Telerik.Web.UI.GridDataItem row)
        {
            int ClientsGroupID = Convert.ToInt32(row.Cells[3].Text);

            //Refresh Data
            GetAllClientsByClientsGroupID(ClientsGroupID);
        }

        void uscClientSearch_OnAddAdminEvent(Telerik.Web.UI.GridDataItem row)
        {
            try
            {
                if (row != null)
                {
                    int ClientsGroupID = uscClientsGroupSearch.GetClientGroupID();
                    if (ClientsGroupID != -1)
                    {
                        //Insert
                        GPSTrackingDBV2.Entities.ClientsByClientsGroups ClientsByClientsGroupsEntity = new GPSTrackingDBV2.Entities.ClientsByClientsGroups();
                       ClientsByClientsGroupsEntity.ClientId = uscClientSearch.GetClientID();
                       ClientsByClientsGroupsEntity.ClientGroupId = ClientsGroupID;

                        GPSTrackingDBV2.Data.DataRepository.ClientsByClientsGroupsProvider.Insert(ClientsByClientsGroupsEntity);

                        //Clear Controls
                        uscClientSearch.ClearControls();

                        //Refresh Data
                        GetAllClientsByClientsGroupID(ClientsByClientsGroupsEntity.ClientGroupId);
                    }
                    else
                    {
                        RadWindowManager1.RadAlert("Debes seleccionar un grupo de clientes", 330, 180, "Error", null, null);
                    }
                }
            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref ClientsGroupsGridLinks);
            }
        }

        protected void ClientsGroupsGridLinks_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                int ClientsGroupID = uscClientsGroupSearch.GetClientGroupID();
                GPSTrackingDBV2.Entities.ClientsByClientsGroups ClientsByClientsGroupsEntity = ClassManager.InvokeEntityManager.Invoke_ClientsByClientsGroups_Entity(e);
                ClientsByClientsGroupsEntity.OriginalClientGroupId = ClientsByClientsGroupsEntity.ClientGroupId = ClientsGroupID;
                ClientsByClientsGroupsEntity.OriginalClientId = ClientsByClientsGroupsEntity.ClientId;
                GPSTrackingDBV2.Data.DataRepository.ClientsByClientsGroupsProvider.Delete(ClientsByClientsGroupsEntity);

                //Refresh Data
                GetAllClientsByClientsGroupID(ClientsByClientsGroupsEntity.ClientGroupId);
            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref ClientsGroupsGridLinks);
            }
        }
    }
}